home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 2 / AACD 2.iso / AACD / Magazine / GraphicsCards / StormMesa / 3Dfx / demos / gltest.c < prev    next >
C/C++ Source or Header  |  1998-12-15  |  12KB  |  521 lines

  1. /*
  2.  * This program is under the GNU GPL.
  3.  * Use at your own risk.
  4.  *
  5.  * written by David Bucciarelli (tech.hmw@plus.it)
  6.  *            Humanware s.r.l.
  7.  */
  8.  
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <math.h>
  13. #include <GL/glut.h>
  14.  
  15. typedef struct {
  16.   char *name;
  17.   char *unit;
  18.   void (*init)(void);
  19.   int (*run)(int, int);
  20.   int type;
  21.   int numsize;
  22.   int size[10];
  23. } benchmark;
  24.  
  25. static int frontbuffer=1;
  26.  
  27. /***************************************************************************/
  28.  
  29. static void init_test01(void)
  30. {
  31.   glMatrixMode(GL_PROJECTION);
  32.   glLoadIdentity();
  33.   gluOrtho2D(-0.5,639.5,-0.5,479.5);
  34.   glMatrixMode(GL_MODELVIEW);
  35.  
  36.   glShadeModel(GL_FLAT);
  37.   glDisable(GL_DEPTH_TEST);
  38.  
  39.   glClearColor(0.0,0.1,1.0,0.0);
  40.   glClear(GL_COLOR_BUFFER_BIT);
  41.   glColor3f(1.0,0.0,0.0);
  42. }
  43.  
  44. static int test01(int size, int num)
  45. {
  46.   int x,y;
  47.  
  48.   glBegin(GL_POINTS);
  49.   for(y=0;y<num;y++)
  50.     for(x=0;x<480;x++)
  51.       glVertex2i(x,x);
  52.   glEnd();
  53.  
  54.   return 480*num;
  55. }
  56.  
  57. /***************************************************************************/
  58.  
  59. static void init_test02(void)
  60. {
  61.   glMatrixMode(GL_PROJECTION);
  62.   glLoadIdentity();
  63.   gluOrtho2D(-0.5,639.5,-0.5,479.5);
  64.   glMatrixMode(GL_MODELVIEW);
  65.  
  66.   glShadeModel(GL_SMOOTH);
  67.   glDisable(GL_DEPTH_TEST);
  68.  
  69.   glClearColor(0.0,0.1,1.0,0.0);
  70.   glClear(GL_COLOR_BUFFER_BIT);
  71. }
  72.  
  73. static int test02(int size, int num)
  74. {
  75.   int x,y;
  76.  
  77.   glBegin(GL_LINES);
  78.   for(y=0;y<num;y++)
  79.     for(x=0;x<size;x++) {
  80.       glColor3f(0.0,1.0,y/(float)num);
  81.       glVertex2i(0,size-1);
  82.       glColor3f(1.0,0.0,x/(float)size);
  83.       glVertex2i(x,x);
  84.     }
  85.   glEnd();
  86.  
  87.   return num*size;
  88. }
  89.  
  90. /***************************************************************************/
  91.  
  92. static void init_test03(void)
  93. {
  94.   glMatrixMode(GL_PROJECTION);
  95.   glLoadIdentity();
  96.   glOrtho(-0.5,639.5,-0.5,479.5,1.0,-1000.0*480.0);
  97.   glMatrixMode(GL_MODELVIEW);
  98.  
  99.   glShadeModel(GL_SMOOTH);
  100.   glEnable(GL_DEPTH_TEST);
  101.  
  102.   glClearColor(0.0,0.1,1.0,0.0);
  103.   glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  104. }
  105.  
  106. static int test03(int size, int num)
  107. {
  108.   int x,y,z;
  109.  
  110.   glBegin(GL_TRIANGLES);
  111.   for(y=0;y<num;y++)
  112.     for(x=0;x<size;x+=5) {
  113.       z=num*size-(y*size+x);
  114.       glColor3f(0.0,1.0,0.0);
  115.       glVertex3i(0,x,z);
  116.  
  117.       glColor3f(1.0,0.0,x/(float)size);
  118.       glVertex3i(size-1-x,0,z);
  119.  
  120.       glColor3f(1.0,x/(float)size,0.0);
  121.       glVertex3i(x,size-1-x,z);
  122.     }
  123.   glEnd();
  124.  
  125.   return size*num/5;
  126. }
  127.  
  128. /***************************************************************************/
  129.  
  130. static void init_test04(void)
  131. {
  132.   int x,y;
  133.   GLubyte tex[128*128*3];
  134.   GLenum gluerr;
  135.  
  136.   glMatrixMode(GL_PROJECTION);
  137.   glLoadIdentity();
  138.   glOrtho(-0.5,639.5,-0.5,479.5,1.0,-1000.0*480.0);
  139.  
  140.   glMatrixMode(GL_MODELVIEW);
  141.  
  142.   glShadeModel(GL_SMOOTH);
  143.   glEnable(GL_DEPTH_TEST);
  144.  
  145.   glEnable(GL_BLEND);
  146.   glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
  147.  
  148.   for(y=0;y<128;y++)
  149.     for(x=0;x<128;x++) {
  150.       tex[(x+y*128)*3+0]=((x % (128/4)) < (128/8)) ? 255 : 0;
  151.       tex[(x+y*128)*3+1]=((y % (128/4)) < (128/8)) ? 255 : 0;
  152.       tex[(x+y*128)*3+2]=x;
  153.     }
  154.  
  155.   glPixelStorei(GL_UNPACK_ALIGNMENT,1);
  156.   if((gluerr=gluBuild2DMipmaps(GL_TEXTURE_2D,3,128,128,GL_RGB,
  157.                    GL_UNSIGNED_BYTE,(GLvoid *)(&tex[0])))) {
  158.     fprintf(stderr,"GLULib%s\n",gluErrorString(gluerr));
  159.     exit(-1);
  160.   }
  161.  
  162.   glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
  163.   glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
  164.  
  165.   glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
  166.   glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
  167.  
  168.   glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
  169.   glEnable(GL_TEXTURE_2D);
  170.  
  171.   glClearColor(0.0,0.1,1.0,0.0);
  172.   glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  173. }
  174.  
  175. static int test04(int size, int num)
  176. {
  177.   int x,y,z;
  178.  
  179.   glBegin(GL_TRIANGLES);
  180.   for(y=0;y<num;y++)
  181.     for(x=0;x<size;x+=5) {
  182.       z=num*size-(y*size+x);
  183.       glTexCoord2f(1.0,1.0);
  184.       glColor3f(1.0,0.0,0.0);
  185.       glVertex3i(0,x,z);
  186.  
  187.       glTexCoord2f(0.0,1.0);
  188.       glColor3f(0.0,1.0,0.0);
  189.       glVertex3i(size-1-x,0,z);
  190.  
  191.       glTexCoord2f(1.0,0.0);
  192.       glColor3f(0.0,0.0,1.0);
  193.       glVertex3i(x,size-1-x,z);
  194.     }
  195.   glEnd();
  196.  
  197.   return num*size/5;
  198. }
  199.  
  200. /***************************************************************************/
  201.  
  202. static void init_test05(void)
  203. {
  204.   int x,y;
  205.   GLubyte tex[128*128*3];
  206.   GLenum gluerr;
  207.  
  208.   glMatrixMode(GL_PROJECTION);
  209.   glLoadIdentity();
  210.   glOrtho(-0.5,639.5,-0.5,479.5,-1.0,1.0);
  211.  
  212.   glMatrixMode(GL_MODELVIEW);
  213.  
  214.   glShadeModel(GL_SMOOTH);
  215.   glEnable(GL_DEPTH_TEST);
  216.  
  217.   glEnable(GL_BLEND);
  218.   glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
  219.  
  220.   for(y=0;y<128;y++)
  221.     for(x=0;x<128;x++) {
  222.       tex[(x+y*128)*3+0]=((x % (128/4)) < (128/8)) ? 255 : 0;
  223.       tex[(x+y*128)*3+1]=((y % (128/4)) < (128/8)) ? 255 : 0;
  224.       tex[(x+y*128)*3+2]=x;
  225.     }
  226.  
  227.   glPixelStorei(GL_UNPACK_ALIGNMENT,1);
  228.   if((gluerr=gluBuild2DMipmaps(GL_TEXTURE_2D,3,128,128,GL_RGB,
  229.                    GL_UNSIGNED_BYTE,(GLvoid *)(&tex[0])))) {
  230.     fprintf(stderr,"GLULib%s\n",gluErrorString(gluerr));
  231.     exit(-1);
  232.   }
  233.  
  234.   glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
  235.   glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
  236.  
  237.   glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
  238.   glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
  239.  
  240.   glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
  241.   glEnable(GL_TEXTURE_2D);
  242.  
  243.   glDepthFunc(GL_ALWAYS);
  244.  
  245.   glClearColor(0.0,0.1,1.0,0.0);
  246.   glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  247. }
  248.  
  249. static int test05(int size, int num)
  250. {
  251.   int y;
  252.   float v0[3]={320-size/2,240-size/2,0.0};
  253.   float v1[3]={320+size/2,240-size/2,0.0};
  254.   float v2[3]={320-size/2,240+size/2,0.0};
  255.   float v3[3]={320+size/2,240+size/2,0.0};
  256.   float cv0[3]={1.0,0.0,0.0};
  257.   float cv1[3]={1.0,1.0,0.0};
  258.   float cv2[3]={1.0,0.0,1.0};
  259.   float cv3[3]={1.0,1.0,1.0};
  260.   float tv0[3]={0.0,0.0};
  261.   float tv1[3]={1.0,0.0};
  262.   float tv2[3]={0.0,1.0};
  263.   float tv3[3]={1.0,1.0};
  264.  
  265.   glBegin(GL_TRIANGLE_STRIP);
  266.   for(y=0;y<num;y++) {
  267.     glColor3fv(cv0);
  268.     glTexCoord2fv(tv0);
  269.     glVertex3fv(v0);
  270.  
  271.     glColor3fv(cv1);
  272.     glTexCoord2fv(tv1);
  273.     glVertex3fv(v1);
  274.  
  275.     glColor3fv(cv2);
  276.     glTexCoord2fv(tv2);
  277.     glVertex3fv(v2);
  278.  
  279.     glColor3fv(cv3);
  280.     glTexCoord2fv(tv3);
  281.     glVertex3fv(v3);
  282.   }
  283.   glEnd();
  284.  
  285.   return 4*num-2;
  286. }
  287.  
  288. /***************************************************************************/
  289.  
  290. static void init_test06(void)
  291. {
  292.   glMatrixMode(GL_PROJECTION);
  293.   glLoadIdentity();
  294.   gluOrtho2D(-0.5,639.5,-0.5,479.5);
  295.   glMatrixMode(GL_MODELVIEW);
  296.  
  297.   glShadeModel(GL_SMOOTH);
  298.   glEnable(GL_DEPTH_TEST);
  299.  
  300.   glClearColor(0.0,0.1,1.0,0.0);
  301.   glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  302. }
  303.  
  304. static int test06(int size, int num)
  305. {
  306.   int y;
  307.  
  308.   for(y=0;y<num;y++) {
  309.     glClearColor(y/(float)num,0.1,1.0,0.0);
  310.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  311.   }
  312.  
  313.   return num;
  314. }
  315.  
  316. /***************************************************************************/
  317.  
  318. #define BMARKS_TIME 5.0
  319.  
  320. #define NUM_BMARKS 6
  321.  
  322. /* 554 ~= sqrt(640*480) */
  323.  
  324. static benchmark bmarks[NUM_BMARKS]={
  325.   {"Simple Points","Pnts",init_test01,test01,0,0,{0,0,0,0,0,0,0,0,0,0}},
  326.   {"Smooth Lines","Lins",init_test02,test02,1,5,{480,250,100,50,25,0,0,0,0,0}},
  327.   {"ZSmooth Triangles","Tris",init_test03,test03,1,5,{480,250,100,50,25,0,0,0,0,0}},
  328.   {"ZSmooth Tex Blend Triangles","Tris",init_test04,test04,1,5,{480,250,100,50,25,0,0,0,0,0}},
  329.   {"ZSmooth Tex Blend TMesh Triangles","Tris",init_test05,test05,2,8,{400,250,100,50,25,10,5,2,0,0}},
  330.   {"Color/Depth Buffer Clears","Clrs",init_test06,test06,3,0,{554,0,0,0,0,0,0,0,0,0}}
  331. };
  332.  
  333. /***************************************************************************/
  334.  
  335. static void dotest0param(benchmark *bmark)
  336. {
  337.   float stime,etime,dtime,tottime,maxtime,mintime;
  338.   int num,numelem,calibnum,j;
  339.  
  340.   glPushAttrib(GL_ALL_ATTRIB_BITS);
  341.   bmark->init();
  342.  
  343.   stime=glutGet(GLUT_ELAPSED_TIME);
  344.  
  345.   dtime=0.0;
  346.   calibnum=0;
  347.   while(dtime<2.0) {
  348.     bmark->run(0,1);
  349.     glFinish();
  350.     etime=glutGet(GLUT_ELAPSED_TIME);
  351.     dtime=(etime-stime)/1000.0;
  352.     calibnum++;
  353.   }
  354.   glPopAttrib();
  355.  
  356.   fprintf(stderr,"Elapsed time for the calibration test (%d): %f\n",calibnum,dtime);
  357.  
  358.   num=(int)((BMARKS_TIME/dtime)*calibnum);
  359.  
  360.   if(num<1)
  361.     num=1;
  362.  
  363.   fprintf(stderr,"Selected number of benchmark iterations: %d\n",num);
  364.  
  365.   mintime=HUGE_VAL;
  366.   maxtime=-HUGE_VAL;
  367.  
  368.   for(tottime=0.0,j=0;j<5;j++) {
  369.     glPushAttrib(GL_ALL_ATTRIB_BITS);
  370.     bmark->init();
  371.  
  372.     stime=glutGet(GLUT_ELAPSED_TIME);
  373.     numelem=bmark->run(0,num);
  374.     glFinish();
  375.     etime=glutGet(GLUT_ELAPSED_TIME);
  376.  
  377.     glPopAttrib();
  378.  
  379.     dtime=(etime-stime)/1000.0;
  380.     tottime+=dtime;
  381.  
  382.     fprintf(stderr,"Elapsed time for run %d: %f\n",j,dtime);
  383.  
  384.     if(dtime<mintime)
  385.       mintime=dtime;
  386.     if(dtime>maxtime)
  387.       maxtime=dtime;
  388.   }
  389.  
  390.   tottime-=mintime+maxtime;
  391.  
  392.   fprintf(stdout,"%s\n%f %s/sec",bmark->name,numelem/(tottime/3.0),bmark->unit);
  393.  
  394.   if(bmark->type==3)
  395.     fprintf(stdout,", MPixel Fill/sec: %f\n\n",
  396.         (numelem*bmark->size[0]*(float)bmark->size[0])/(1000000.0*tottime/3.0));
  397.   else
  398.     fprintf(stdout,"\n\n");
  399. }
  400.  
  401. /***************************************************************************/
  402.  
  403. static void dotest1param(benchmark *bmark)
  404. {
  405.   float stime,etime,dtime,tottime,maxtime,mintime;
  406.   int num,numelem,calibnum,j,k;
  407.  
  408.   fprintf(stdout,"%s\n",bmark->name);
  409.  
  410.   for(j=0;j<bmark->numsize;j++) {
  411.     fprintf(stderr,"Current size: %d\n",bmark->size[j]);
  412.  
  413.     glPushAttrib(GL_ALL_ATTRIB_BITS);
  414.     bmark->init();
  415.  
  416.     stime=glutGet(GLUT_ELAPSED_TIME);
  417.  
  418.     dtime=0.0;
  419.     calibnum=0;
  420.     while(dtime<2.0) {
  421.       bmark->run(bmark->size[j],1);
  422.       glFinish();
  423.       etime=glutGet(GLUT_ELAPSED_TIME);
  424.       dtime=(etime-stime)/1000.0;
  425.       calibnum++;
  426.     }
  427.     glPopAttrib();
  428.  
  429.     fprintf(stderr,"Elapsed time for the calibration test (%d): %f\n",calibnum,dtime);
  430.  
  431.     num=(int)((BMARKS_TIME/dtime)*calibnum);
  432.  
  433.     if(num<1)
  434.       num=1;
  435.  
  436.     fprintf(stderr,"Selected number of benchmark iterations: %d\n",num);
  437.  
  438.     mintime=HUGE_VAL;
  439.     maxtime=-HUGE_VAL;
  440.  
  441.     for(numelem=1,tottime=0.0,k=0;k<5;k++) {
  442.       glPushAttrib(GL_ALL_ATTRIB_BITS);
  443.       bmark->init();
  444.  
  445.       stime=glutGet(GLUT_ELAPSED_TIME);
  446.       numelem=bmark->run(bmark->size[j],num);
  447.       glFinish();
  448.       etime=glutGet(GLUT_ELAPSED_TIME);
  449.  
  450.       glPopAttrib();
  451.  
  452.       dtime=(etime-stime)/1000.0;
  453.       tottime+=dtime;
  454.  
  455.       fprintf(stderr,"Elapsed time for run %d: %f\n",k,dtime);
  456.  
  457.       if(dtime<mintime)
  458.     mintime=dtime;
  459.       if(dtime>maxtime)
  460.     maxtime=dtime;
  461.     }
  462.  
  463.     tottime-=mintime+maxtime;
  464.  
  465.     fprintf(stdout,"SIZE=%03d => %f %s/sec",bmark->size[j],numelem/(tottime/3.0),bmark->unit);
  466.     if(bmark->type==2)
  467.       fprintf(stdout,", MPixel Fill/sec: %f\n",
  468.           (numelem*bmark->size[j]*bmark->size[j]/2)/(1000000.0*tottime/3.0));
  469.     else
  470.       fprintf(stdout,"\n");
  471.   }
  472.  
  473.   fprintf(stdout,"\n\n");
  474. }
  475.  
  476. /***************************************************************************/
  477.  
  478. static void display(void)
  479. {
  480.   int i;
  481.  
  482.   if(frontbuffer)
  483.     glDrawBuffer(GL_FRONT);
  484.   else
  485.     glDrawBuffer(GL_BACK);
  486.  
  487.   for(i=0;i<NUM_BMARKS;i++) {
  488.     fprintf(stderr,"Benchmark: %d\n",i);
  489.  
  490.     switch(bmarks[i].type) {
  491.     case 0:
  492.     case 3:
  493.       dotest0param(&bmarks[i]);
  494.       break;
  495.     case 1:
  496.     case 2:
  497.       dotest1param(&bmarks[i]);
  498.       break;
  499.     }
  500.   }
  501.  
  502.   exit(0);
  503. }
  504.  
  505. int main(int ac, char **av)
  506. {
  507.   fprintf(stderr,"GLTest v1.0\nWritten by David Bucciarelli\n");
  508.  
  509.   if(ac==2)
  510.     frontbuffer=0;
  511.  
  512.   glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
  513.   glutInitWindowPosition(0,0);
  514.   glutInitWindowSize(640,480);
  515.   glutCreateWindow("OpenGL/Mesa Performances");
  516.   glutDisplayFunc(display);
  517.   glutMainLoop();
  518.  
  519.   return 0;
  520. }
  521.